<?php

namespace app\common\business;
use \app\common\model\mysql\Auth as authMd;
use think\Exception;

class Auth
{
    public function __construct()
    {
        $this->model = new authMd();
    }

    public function authList($params)
    {
        $where = [];

        $getAuthList = $this->model->getSelect($where,'id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')->toArray();
        if(empty($getAuthList)){
            return [];
        }

        if(!empty($params['type']) && $params['type'] == 'tree'){
            //父子级树状列表
            $list =  get_tree_list($getAuthList);
        }else{
            //无限极分类列表
            $list = get_cate_list($getAuthList);
        }
        return $list;

    }

    public function authRead($id)
    {
        $result = $this->model->field('id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')->find($id);
        if(empty($result)){
            return [];
        }
        return $result->toArray();
    }

    public function authSave($params)
    {
        $isExists = $this->model->getSelect([['auth_name','=',$params['auth_name']]])->toArray();
        if(!empty($isExists)){
            throw new Exception('当前权限已存在');
        }
        if($params['pid'] == 0){
            $params['pid_path'] = 0;
            $params['level'] = 0;
            $params['sort'] = 50;
        }else{
            $getDataByPid = $this->model->find($params['pid']);
            if(empty($getDataByPid)){
                throw new Exception('没有当前pid父级');
            }
            $getDataByPid = $getDataByPid->toArray();
            $params['pid_path'] = $getDataByPid['pid_path'].'_'.$params['pid'];
            $params['level'] = $getDataByPid['level']+1;
            $params['sort'] = 50;

        }

        $id = $this->model->insertNewData($params);
        if(empty($id)){
            throw new Exception('插入失败');
        }
        $params['id'] = $id;
        return $params;


    }

    /**
     * @param $id
     * @param $params
     * @return authMd|array|false|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 权限更新
     */
    public function authUpdate($id,$params)
    {
        $pid = $params['pid'];

        $auth = $this->model->find($id);
        if(empty($auth)){
            return false;
        }

        if($pid==0){
            //如果修改顶级权限
            $params['level'] = 0;
            $params['pid_path'] = 0;
        }else if($pid != $auth['pid']){
            //如果修改其上级权限pid 重新设置level级别和pid_path家族图谱
            $p_auth = $this->model->find($pid);
            if(empty($p_auth)){
                return false;
            }
            $params['level'] = $p_auth['level']+1;
            $params['pid_path'] = $p_auth['pid_path'].'_'.$p_auth['id'];

        }
        $this->model->updateNewData($params,['id'=>$id]);
        //返回数据
        $info = $this->model->find($id);
        return $info;


    }

    public function authDelete($id){
        //判断是否有子权限
        $total = $this->model->where('pid',$id)->count();
        if($total>0){
            return false;
        }
        //删除数据
        $this->model->destroy($id);
        return true;

    }

    public function getNavList()
    {
        //判断is_nav等于1的就是菜单权限
        $navList = $this->model->getSelect([['is_nav','=',1]],'id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')->toArray();
        if(empty($navList)){
            return false;
        }
        $result = get_tree_list($navList);
        halt($result);

    }

    public function getAuthsByRoles($where){
        return $this->model->getSelect($where);
    }

}